﻿Imports System.Data

Partial Class Account_CreateOrganization
    Inherits System.Web.UI.Page

    Dim conString As New OleDb.OleDbConnection
    Dim dataSet As New DataSet
    Dim dataAdapter As New OleDb.OleDbDataAdapter
    Dim dbProvider As String
    Dim dbSource As String
    Dim dbSource2 As String
    Dim sqlCommand As String
    Dim MaxRows As Integer
    Dim rows As Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"

        dbSource = "Data Source = "
        dbSource2 = Server.MapPath("~/Database/ImagineCup.mdb")

        'connection string to the global db, ImagineCup.
        conString.ConnectionString = dbProvider & dbSource & dbSource2

        conString.Open()

        sqlCommand = "SELECT * FROM ImagineCup_Org"
        dataAdapter = New OleDb.OleDbDataAdapter(sqlCommand, conString)
        dataAdapter.Fill(dataSet, "OrganizationTbl")

        conString.Close()

        MaxRows = dataSet.Tables("OrganizationTbl").Rows.Count
        rows = MaxRows - 1

        lblUserNameAuto.Text = Membership.GetUser.UserName

    End Sub

    Protected Sub btnCreateOrganization_Click(sender As Object, e As System.EventArgs) Handles btnCreateOrganization.Click

        Dim UserLogged As String
        UserLogged = Membership.GetUser().UserName
        Dim Matched As Integer

        conString.Open()

        Dim sqlSearch As String = "SELECT * FROM ImagineCup_Org WHERE UserName = '" & UserLogged & "'"
        Dim dataAdapter = New OleDb.OleDbDataAdapter(sqlSearch, conString)
        dataAdapter.Fill(dataSet, "OrganizationIN")

        conString.Close()

        MaxRows = dataSet.Tables("OrganizationIN").Rows.Count

        Matched = MaxRows - 1

        MsgBox(Matched)

        If Matched > 0 Then

            lblValidate.Text = "You are already a founder of an organization, you cannot add multiple organizations."

        Else

            Dim dataRow As DataRow
            Dim commandString As String

            commandString = "INSERT INTO ImagineCup_Org (org_Name, Address, contact_Person, telephone_Number, fax_Number, Location, Founder, Email, UserName, est_Date) VALUES (" + "'" + txtOrganizationName.Text + "'," + "'" + txtAddress.Text + "'," + "'" + txtContactPerson.Text + "'," + "'" + txtTelephoneNo.Text + "'," + "'" + txtFaxNo.Text + "'," + "'" + txtLocation.Text + "'," + "'" + txtFounder.Text + "'," + "'" + txtEmail.Text + "'," + "'" + UserLogged + "'" + ",'" + Date.Now + "'" + ")"

            Dim sqlInsert As OleDb.OleDbCommand = New OleDb.OleDbCommand(commandString, conString)

            dataRow = dataSet.Tables("OrganizationIN").NewRow()

            dataSet.Tables("OrganizationIN").Rows.Add(dataRow)

            conString.Open()
            sqlInsert.ExecuteNonQuery()
            conString.Close()

            lblValidate.Text = "Organization Created!"

        End If

    End Sub
End Class
